package top.liujiachen.springboot.shiro.dao.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import top.liujiachen.springboot.shiro.dao.IRoleDao;
import top.liujiachen.springboot.shiro.entity.SysRole;
import top.liujiachen.springboot.shiro.mapper.SysRoleMapper;

import java.util.Map;
import java.util.Optional;

@Service
public class RoleDao extends ServiceImpl<SysRoleMapper, SysRole> implements IRoleDao {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public SysRole getById(Long id) {
        SysRole sysRole = null;
        if (Optional.ofNullable(id).isPresent()) {
            try {
                Wrapper wrapper = lambdaQuery().eq(SysRole::getId, id).getWrapper();
                sysRole = getOne(wrapper);
            } catch (Exception e) {
                logger.error("MyBatis-Plus Error: ", e);
            }
        }
        return sysRole;
    }

    @Override
    public String getCodeById(Long id) {
        String code = null;
        if (Optional.ofNullable(id).isPresent()) {
            try {
                Wrapper wrapper = lambdaQuery().select(SysRole::getCode).eq(SysRole::getId, id).getWrapper();
                Map map = getMap(wrapper);
                if (Optional.ofNullable(map).isPresent()) {
                    code = (String) map.get("code");
                }
            } catch (Exception e) {
                logger.error("MyBatis-Plus Error: ", e);
            }
        }
        return code;
    }
}
